﻿Imports System.Net.Mail
Partial Class WebForm_SelfManageWorkHeader_SendMail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim CasePassword As String = Request.QueryString("CasePassword")
            Dim SentContent As String = ""
            Dim dt As Data.DataTable
            Dim dat As New clsData
            Dim sql As New MyStringBuilder

            sql.Length = 0
            sql.AppendFormat(" Select a.*,b.Text,c.RegionName ")
            sql.AppendFormat(" ,d.RoadItemName ")
            sql.AppendFormat(" ,Case a.RegionType when '{0}' then '' else e.RoadName end as RoadName", "道路")
            sql.AppendFormat(" from FD_FindDirtyHeader a")
            sql.AppendFormat(" left join FD_CodeMapping b on b.TableName='FindDirtyHeader' and b.ColumnName='Status' and a.Status=b.Value ")
            sql.AppendFormat(" Join Region c on a.TownID=c.RegionID")
            sql.AppendFormat(" left Join FD_RoadItem d ")
            sql.AppendFormat(" on d.RoadItemID = a.RoadItemID")
            sql.AppendFormat(" left Join RegionRoad e ")
            sql.AppendFormat(" on e.RoadID = a.VillageID")
            sql.AppendFormat(" where a.CasePassword='{0}'", CasePassword)

            dt = dat.GetData(sql)
            If dt.Rows.Count > 0 Then
                Me.txtTopic.Text = "台北縣環保局環境污染通報系統通知"
                Me.txtCreateEmail.Text = dt.Rows(0)("CreateEmail").ToString()
                SentContent = dt.Rows(0)("CreateBy").ToString() & " 您好："
                SentContent &= vbCrLf & vbCrLf & "案件代碼：" & dt.Rows(0)("RandomCasePassword").ToString()
                SentContent &= vbCrLf & "轄區：" & dt.Rows(0)("RegionName").ToString()
                SentContent &= vbCrLf & "地點：" & dt.Rows(0)("RoadName").ToString() & dt.Rows(0)("RoadItemName").ToString() & dt.Rows(0)("Location").ToString()
                SentContent &= vbCrLf & "主旨：" & dt.Rows(0)("Topic").ToString()
                SentContent &= vbCrLf & "髒亂描述：" & dt.Rows(0)("Description").ToString()
                SentContent &= vbCrLf & "案件狀態：" & dt.Rows(0)("Text").ToString()
                SentContent &= vbCrLf & "辦理情形：" & dt.Rows(0)("Memo").ToString()
                SentContent &= vbCrLf & vbCrLf & "台北縣環保局  敬上"
                Me.txtSendContent.Text = SentContent
            End If
        End If
    End Sub

    Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Try
            Dim objSMTP As New SmtpClient()
            Dim objMailMessage As New MailMessage()

            objMailMessage.From = New MailAddress(MyObj.GetParameter("Home", "MailSender"))
            objMailMessage.To.Add(New MailAddress(Me.txtCreateEmail.Text.Trim()))
            objMailMessage.Subject = Me.txtTopic.Text.Trim()
            objMailMessage.SubjectEncoding = System.Text.Encoding.UTF8
            objMailMessage.Body = Me.txtSendContent.Text.Replace(vbCrLf, "<br>")
            objMailMessage.BodyEncoding = System.Text.Encoding.UTF8
            objMailMessage.IsBodyHtml = True
            objSMTP.Send(objMailMessage)
            Dim strScript As String = "alert('通知信件已寄出!!');window.close();"
            ScriptManager.RegisterClientScriptBlock(TryCast(HttpContext.Current.Handler, Page), GetType(String), strScript, strScript, True)

        Catch ex As Exception
            MyObj.ShowMessage("通知信件無法寄送,請洽系統管理員!!")
            MyObj.WriteLog(ex.ToString())
        End Try
    End Sub
End Class
